package name.fowler.refactoring.simplestore.services;

import name.fowler.refactoring.simplestore.domain.Customer;
import name.fowler.refactoring.simplestore.domain.Rental;

import javax.faces.bean.ApplicationScoped;
import java.util.List;

/**
 * User: RedSmog
 * Date: 12.10.13
 * Time: 16:37
 */

@ApplicationScoped
public class ReportAllDebitors implements Report {
    @Override
    public String getReportTitle() {
        return "List of debtors:\n";
    }

    @Override
    public String getReportFooter() {
        return "Total amount owed is ";
    }

    @Override
    public String getCustomerFooter(Customer customer, Formatter formatter) {
        double customerAmount =0.0;
        for (Rental each : customer.getRentals()) {
            customerAmount += each.getAmount();
        }
        return customerAmount + "\n";
    }

    @Override
    public String getCustomerHeader(Customer customer) {
        return "\t" + customer.getName() + " amount owed is ";
    }

    @Override
    public String getTotal(List<Customer> customers) {
        double total = 0.0;
        for (Customer customer : customers) {
            for (Rental rental : customer.getRentals()) {
                total += rental.getAmount();
            }
        }
        return String.valueOf(total);
    }

    @Override
    public boolean isPrintDebitor(Customer customer) {
        boolean res=false;
        for (Rental rental : customer.getRentals()) {
            if (rental.getAmount() > 0) {
                res = true;
            }
        }
        return res;
    }
}
